package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.WantGoodsCollection;
import cn.duli.xianer.service.WantGoodsCollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wantGoodsCollection")
public class WantGoodsCollectionController {

    @Resource
    private WantGoodsCollectionService wantGoodsCollectionService;


    /**
     * 新增收藏
     * @param wantGoodsCollection
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult save( WantGoodsCollection wantGoodsCollection) {
        wantGoodsCollectionService.save(wantGoodsCollection);
        return new MyResult(200, "新增收藏",null,null,null,null);
    }

    /**
     * 根据id查询收藏详情
     * @param wgcId
     */
    @GetMapping("/findById/{wgcId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findById(@PathVariable("wgcId") Integer wgcId) {
        WantGoodsCollection wantGoodsCollection = wantGoodsCollectionService.findById(wgcId);
        return new MyResult(200, "根据id查询收藏详情",null,null,wantGoodsCollection,null);
    }

    /**
     * 根据商品id和用户id查看该商品的收藏信息
     * @param wgcWgid
     * @param wgcUid
     * @return
     */
    @GetMapping("/findByAllId")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findByAllId(@RequestParam("wgcWgid") Integer wgcWgid,
                                @RequestParam("wgcUid") Integer wgcUid) {
        WantGoodsCollection wantGoodsCollection = wantGoodsCollectionService.findByAllId(wgcWgid, wgcUid);
        return new MyResult(200, "根据id查询收藏详情",null,null,wantGoodsCollection,null);
    }

    /**
     * 根据用户id和商品id删除（取消）此收藏
     * @param wgcId
     */
    @RequestMapping("/deleteById/{wgcId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult deleteById(@PathVariable("wgcId") Integer wgcId) {
        wantGoodsCollectionService.deleteById(wgcId);
        return new MyResult(200, "根据id删除（取消）此收藏",null,null,null,null);
    }

    /**
     * 根据用户id查看该用户收藏的求购商品列表
     * @param wgcUid
     * @return
     */
    @RequestMapping("/findByUserId")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findByUserId(@RequestParam("wgcUid") Integer wgcUid,
                                 @RequestParam(defaultValue = "1") int pageNo,
                                 @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoodsCollection> pageInfo = new PageInfo<>(wantGoodsCollectionService.findByUserId(wgcUid));
        return new MyResult(200, "根据用户id查看该用户收藏的商品列表",null,null,null,pageInfo);
    }

    /**
     * 根据求购商品id和用户id查看该商品是否被收藏
     * @param wgcWgid
     * @param wgcUid
     * @return
     */
    @RequestMapping("/isCollection")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult isCollection(Integer wgcWgid, Integer wgcUid) {
        boolean result = wantGoodsCollectionService.isCollection(wgcWgid, wgcUid);
        return new MyResult(200, "根据求购商品id和用户id查看该商品是否被收藏",null,null,result,null);
    }

    /**
     * 分页查询所有收藏的求购商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                            @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoodsCollection> pageInfo = new PageInfo<>(wantGoodsCollectionService.findAll());
        return new MyResult(200, "分页查询所有收藏的求购商品列表",null,null,null,pageInfo);
    }
}
